# !/usr/bin/env python
# -*- coding:utf-8 -*-

import smtplib
import time
from email.header import Header
from email.mime.text import MIMEText

from loguru import logger


def get_html(datas):
    color_arr = [
        '#FFB6C1',
        '#0000FF',
        '#FFA500',
        '#5C3317',
        '#B0C4DE',
        '#87CEEB',
        '#FFB6C1',
    ]
    header = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>'
    th = f"<body text='#000000'><table bgcolor='#FFFFFF' style=' font-size: 11px;'border='1' cellspacing='0' cellpadding='0' bordercolor='#FFFFFF' width='95%' align='center' >" \
         "<tr  bgcolor='#FFFFFF' align='left' >" \
         f"<th style='width:200px'>低于支撑</th>" \
         f"<th style='width:200px'>名称-代码</th>" \
         f"<th style='width:200px'>涨幅</th>" \
         f"<th style='width:200px'>当前/支撑</th>" \
         "</tr>"
    tr = ""
    pre_dis_low_zc = 0
    bg_color = color_arr[0]
    for index, item in enumerate(datas):
        sname = item.get('名称')
        snumber = item.get('代码')
        dis_low_zc = item.get('低于支撑数')
        zczf = item.get('支撑涨幅')
        cur_price = item.get('价格')
        indus = item.get('行业')
        zc_date = item.get('支撑日期')
        zc_price = item.get('支撑价格')
        if dis_low_zc != pre_dis_low_zc:
            bg_color = color_arr[index % len(color_arr)]
            pre_dis_low_zc = dis_low_zc
        stock_number_arr = snumber.split('.')
        stock_number = f'{stock_number_arr[1]}{stock_number_arr[0]}'
        zc_info = f'{indus}<br>{zc_date}<br>{cur_price}/{zc_price}'
        h_a = f"<a href='https://xueqiu.com/S/{stock_number}'>{sname}-{stock_number}</a>"
        td = ''
        td = td + f"<td style='background-color:{bg_color}'>" + str(dis_low_zc) + "</td>"
        td = td + f"<td style='background-color:{bg_color}'>" + str(h_a) + "</td>"
        td = td + f"<td style='background-color:{bg_color}'>" + f'{str(zczf)}%' + "</td>"
        td = td + f"<td style='background-color:{bg_color}'>" + str(zc_info) + "</td>"
        tr = tr + "<tr>" + td + "</tr>"

    body = str(tr)
    tail = f'</table></body></html>'
    html_txt = header + th + body + tail
    return html_txt


def send_html(datas, title):
    # 邮箱服务器地址
    # 发件人邮箱地址
    # 发件人邮箱密码(使用申请的客户端授权码代替)
    smtp_server = 'smtp.qq.com'
    from_addr = '83852084@qq.com'
    # passwd = 'ruqbqiqjmnjhbgih'
    passwd = 'iqjzamocyiaibjfg'

    # 收件人邮箱地址
    to_addr = 'stockbing@icloud.com'

    # 构建邮件内容(参数:邮件内容; 类型-plain,html; 编码)
    # msg = MIMEText(str(data), 'plain', 'utf-8')
    html = get_html(datas)
    msg = MIMEText(html, _subtype='html', _charset='utf-8')

    # 设置邮件主题
    msg['Subject'] = Header(f'{title}持仓股分析', charset='utf-8')
    msg['from'] = from_addr
    msg['to'] = to_addr

    # 获取SMTP对象
    # server = smtplib.SMTP(host=smtp_server, port=25)
    server = smtplib.SMTP_SSL(host=smtp_server, port=465)
    # 登录163邮箱服务器
    server.login(user=from_addr, password=passwd)
    # 发送邮件
    server.sendmail(from_addr=from_addr, to_addrs=to_addr, msg=msg.as_string())
    # 退出邮箱服务器
    server.quit()
    logger.info(f'{title} 邮件发送成功')
